#include "ResourceManager.h"

Pair<Resource*, unsigned int>* ResourceManager::registerResource(Resource* r)
{
	//check if resource is already registered
	unsigned int i = 0;
	for(i =0; i < resources.size(); i++)
		if(r->getID() == resources[i].getID())
			return &(resources[i]);
	resources.push_back(Pair<Resource*, unsigned int>(r, 0));
	//create a user vector for the resource
	resourceUsers.push_back(Pair<Resource*, Vector<unsigned int>>(
				r, Vector<unsigned int>());
	userStates.push_back(Pair<Resource*, Vector<unsigned char>>(
				r, Vector<unsigned char>());
	return &(resources[resources.size()-1]);
}

Pair<Resource*, Vector<unsigned int>>* Resource::registerResourceUser(Resource* r,
						unsigned int user)
{
	//find out if the resource has been registered.
	unsigned int i = 0;
	for(i = 0; i < resources.size(); i++)
		if(r->getID()==resources[i].getID())
			break;
	unsigned int j = 0;
	for(j = 0; j < resourceUsers[i].size(); j++)
		if(user == resourceUsers[i][j]) //user already registered
			return;
	//registers the user to the vector.
	resourceUsers[i].push_back(user);
	userStates[i].push_back(ResourceManager::IDLE); //idle.
	//return the pair with the user id
	return &(resourceUsers[i]); 
}

Pair<Resource*, unsigned int>* ResourceManager::getResource(Resource* r)
{
	unsigned int i = 0;
	for(i = 0; i < resources.size(); i++)
	{
		if(resources[i].first->getID()==r->getID())
			return &(resources[i]);
	}
	return NULL; //nothing of this resource.
}

Pair<Resource*, Vector<unsigned int>>* ResourceManager::getResourceUser(
								Resource* r)
{
	
	unsigned int i = 0;
	for(i = 0; i < resources.size(); i++)
		if(resources[i].first->getID()==r->getID())
			break;	
	//I have the index I need return the resource users now.
	return &(resourceUsers[i]);
}
	
Pair<Resource*, Vector<unsigned char>>* ResourceManager::getResourceStates(
					Resource* r)
{
	unsigned int i = 0;
	for(i = 0; i < resources.size(); i++)
		if(resources[i].first->getID() == r->getID())
			break;
	return &(userStates[i]);
}
